/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-24 20:07:05
 * @LastEditTime: 2022-08-20 20:30:52
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




extern void H_NES_Mapper2_PRG_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper2_PRG_Read(Huint16 Address,void* v);
extern Huint16 H_NES_Mapper2_PRG_Read16(Huint16 Address,void* v);
extern void H_NES_Mapper0_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v);
#define H_NES_Mapper2_CHR_Exception_Write H_NES_Mapper0_CHR_Exception_Write
extern void H_NES_Mapper0_CHR_Write(Hbyte Value,Huint16 Address,void* v);
#define H_NES_Mapper2_CHR_Write H_NES_Mapper0_CHR_Write
extern Hbyte H_NES_Mapper0_CHR_Read(Huint16 Address,void* v);
#define H_NES_Mapper2_CHR_Read H_NES_Mapper0_CHR_Read




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper2




static void GetProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  Hbyte* Base;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultGetProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  Base=Console->Cartridge.Mapper.PRG_ROM;
  p[0]=(Hbyte)(((Huint32)(Console->ThisMapper.PRG_Banks.Base8000_16KB[0]-Base))/0x4000U);

}

static void LoadProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultLoadProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  Console->ThisMapper.PRG_Banks.Base8000_16KB[0]=&Console->Cartridge.Mapper.PRG_ROM[(((Huint32)p[0])*0x4000U)%Console->Cartridge.Mapper.PRG_ROM_Size];

}

int H_NES_Mapper2_Init(void* v){
  H_NES_Console* Console;

  Console=v;


  if(Console->Cartridge.Mapper.PRG_RAM_Size<0){
    Console->Cartridge.Mapper.PRG_RAM_Size=0x2000;
    Console->Cartridge.Mapper.PRG_RAM=Console->Memory.Malloc(0x2000);
    if(Console->Cartridge.Mapper.PRG_RAM==NULL){
      return vH_NES_Result_PRG_RAM_AllocFail;
    }
  }


  Console->ThisMapper.PRG_Banks.Base8000_16KB[0]=Console->Cartridge.Mapper.PRG_ROM;
  Console->ThisMapper.PRG_Banks.Base8000_16KB[1]=&Console->Cartridge.Mapper.PRG_ROM[Console->Cartridge.Mapper.PRG_ROM_Size-0x4000U];


  Console->Cartridge.Mapper.DeInit=NULL;

  
  Console->Cartridge.PRG_Write=H_NES_Mapper2_PRG_Write;
  Console->Cartridge.PRG_Read=H_NES_Mapper2_PRG_Read;
  Console->Cartridge.PRG_Read16=H_NES_Mapper2_PRG_Read16;
  
  if(Console->Cartridge.Mapper.CHR_ROM_Size==0U){
    Console->Cartridge.CHR_Write=H_NES_Mapper2_CHR_Write;
  }else{
    Console->Cartridge.CHR_Write=H_NES_Mapper2_CHR_Exception_Write;
  }
  Console->Cartridge.CHR_Read=H_NES_Mapper2_CHR_Read;

  Console->Cartridge.Progress.Size=1+H_NES_Mapper_DefaultProgressSize(Console);
  Console->Cartridge.Progress.Get=GetProgress;
  Console->Cartridge.Progress.Load=LoadProgress;
  
  

  return vH_NES_Result_OK;
}




#undef ThisMapper



